package com.kingdee.eas.hr.compensation.app;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.common.server.CanonUtils;
import com.kingdee.eas.framework.report.util.SqlParams;
import com.kingdee.eas.hr.canon.cbase.GradeInfo;
import com.kingdee.eas.hr.compensation.FixedCmpSchemeInfo;
import com.kingdee.eas.hr.compensation.util.HRUtils;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class FixedCmpSchemeFacadeControllerBeanEx extends
		FixedCmpSchemeFacadeControllerBean {

	@Override
	protected void _auditePerson(Context ctx, IObjectPK pk, List list)
			throws BOSException, EASBizException {
		super._auditePerson(ctx, pk, list);
		// this.updatePayGrade(ctx, pk, list);
	}

	@Override
	protected void _enablePerson(Context ctx, IObjectPK pk, List list)
			throws BOSException, EASBizException {
		super._enablePerson(ctx, pk, list);
		this.updatePayGrade(ctx, pk, list);
	}

	private void updatePayGrade(Context ctx, IObjectPK pk, List list)
			throws EASBizException, BOSException {
		FixedCmpSchemeInfo info = HRUtils.getFixedCmpScheme(ctx);
		String table = info.getHorTableName();

		Iterator it = list.iterator();
		String perId = null;
		int i = 0;

		StringBuffer sql = new StringBuffer();
		sql.append("/*dialect*/SELECT A.FPERSONID AS PERSONID,				\n");
		sql.append("		       FSAL001 AS FSAL001,			\n");
		sql.append("		       C.FID AS GRADEID,			\n");
		sql.append("		       C.CFBASEMAX AS BASEMAX,		\n");
		sql.append("		       C.CFBASEEX AS BASEEX,			\n");
		sql.append("		       C.CFBASE3RD AS BASE3RD,			\n");
		sql.append("		       C.CFBASEMID AS BASEMID,			\n");
		sql.append("		       C.CFBASE2ND AS BASE2ND,			\n");
		sql.append("		       C.CFBASEENTRY AS BASEENTRY		\n");
		sql.append("		  FROM " + table + " A 				\n");
		sql.append("		  LEFT JOIN T_BD_PERSON B			\n");
		sql.append("		    ON A.FPERSONID = B.FID			\n");
		sql.append("		  LEFT JOIN T_CAN_GRADE C			\n");
		sql.append("		    ON B.FGRADEID = C.FID			\n");
		sql.append("		  WHERE A.FPERSONID IN (?,?,?,?,?,?,?,?,?,?)	\n");

		SqlParams params = new SqlParams();

		String updateSql = "/*dialect*/UPDATE T_HR_PERSONPOSITION SET CFPAYGRADE = ? WHERE FPERSONID=?";

		
		List upList = new ArrayList<SqlParams>();
		try {
			while (it.hasNext()) {
				perId = (String) it.next();
				i++;
				params.addString(perId);
				if (i >= 10 || !it.hasNext()) {

					for (int k = i + 1; k <= 10; k++) {
						params.addString(null);
					}

					IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(),
							params.getParams());

					while (rs.next()) {
						String person = rs.getString("PERSONID");
						BigDecimal salary = rs.getBigDecimal("FSAL001");
						String gradeid = rs.getString("GRADEID");

						BigDecimal basemax = rs.getBigDecimal("BASEMAX");
						BigDecimal baseex = rs.getBigDecimal("BASEEX");
						BigDecimal base3rd = rs.getBigDecimal("BASE3RD");
						BigDecimal basemid = rs.getBigDecimal("BASEMID");
						BigDecimal base2nd = rs.getBigDecimal("BASE2ND");
						BigDecimal baseentry = rs.getBigDecimal("BASEENTRY");

						GradeInfo grade = new GradeInfo();
						grade.setId(BOSUuid.read(gradeid));
						grade.setBaseMax(basemax);
						grade.setBaseEx(baseex);
						grade.setBase3rd(base3rd);
						grade.setBase2nd(base2nd);
						grade.setBaseMid(basemid);
						grade.setBaseEntry(baseentry);

						String payGrade = CanonUtils.getPayGrade(ctx, salary,
								grade);

						System.out.println("==========" + payGrade);
						SqlParams upParams = new SqlParams();
						upParams.addString(payGrade);
						upParams.addString(person);
						upList.add(upParams.getParams());
					}

					i = 0;
					params = new SqlParams();
				}
			}
			DbUtil.executeBatch(ctx, updateSql, upList);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BOSException(e);
		}

	}
}
